package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.SysMenu;
import com.chhuang.ewcrm.entity.SysRoleMenu;
import com.chhuang.ewcrm.service.SysMenuService;
import com.chhuang.ewcrm.service.SysRoleMenuService;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (SysMenu)表控制层
 *
 * @author chhuang
 * @since 2020-10-23 11:09:29
 */
@RestController
@RequestMapping("/service/sysMenu")
public class SysMenuController {
    Logger logger = LoggerFactory.getLogger(SysMenuController.class);
    /**
     * 服务对象
     */
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 获取所有一级菜单,可点的不可点的都有
     * @return
     */
    @GetMapping("/getMainMenu")
    public ResultBean<List<SysMenu>> getMainMenu(){
        ResultBean<List<SysMenu>> resultBean = new ResultBean<>();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentMenuId("fa-");
        List<SysMenu> menus = this.sysMenuService.queryLike(sysMenu, "menu_order");
        resultBean.setData(menus);
        resultBean.setSuccess(true);
        return resultBean;
    }

    /**
     * 获取所有父级菜单/不可点击菜单
     * @return
     */
    @GetMapping("/getParentMenu")
    public ResultBean<List<SysMenu>> getParentMenu(){
        ResultBean<List<SysMenu>> resultBean = new ResultBean<>();
        List<SysMenu> menus = this.sysMenuService.getMenuUrlIsNull();
        resultBean.setData(menus);
        resultBean.setSuccess(true);
        return resultBean;
    }

    /**
     * 获取所有可点击菜单
     * @return
     */
    @GetMapping("/getURLMenu")
    public ResultBean<List<SysMenu>> getURLMenu(){
        ResultBean<List<SysMenu>> resultBean = new ResultBean<>();
        List<SysMenu> menus = this.sysMenuService.getMenuUrlIsNotNull();
        resultBean.setData(menus);
        resultBean.setSuccess(true);
        return resultBean;
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/removeOne/{menuId}")
    public ResultBean<?> removeOne(@PathVariable("menuId") String menuId){
        ResultBean<?> resultBean = new ResultBean<>();
        if(StringUtils.isNotEmpty(menuId)){
            resultBean.setSuccess(this.sysMenuService.deleteById(menuId));
            if(resultBean.isSuccess()) {//删除关联菜单
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(menuId);
                this.sysRoleMenuService.deleteByEntity(sysRoleMenu);
            }
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 根据id列表批量删除
     * @param menuIds
     * @return
     */
    @PutMapping("/removeList")
    public ResultBean<?> removeList(@RequestBody List<String> menuIds){
        ResultBean<?> resultBean = new ResultBean<>();
        if(menuIds!=null && !menuIds.isEmpty()){
            resultBean.setSuccess(this.sysMenuService.deleteByIds(menuIds)>0);
            //删除菜单关联表
            if(resultBean.isSuccess()){
                menuIds.forEach(id->{
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setRoleId(id);
                    this.sysRoleMenuService.deleteByEntity(sysRoleMenu);
                });
            }
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 修改菜单信息
     * @param sysMenu
     * @return
     * @throws IOException
     */
    @PutMapping("/editOne")
    public ResultBean<SysMenu> editOne(@RequestBody SysMenu sysMenu){
        ResultBean<SysMenu> resultBean = new ResultBean<SysMenu>();
        resultBean.setSuccess(false);
        Date now = new Date();
        if(sysMenu!=null && StringUtils.isNotEmpty(sysMenu.getMenuName())) {
            if (StringUtils.isEmpty(sysMenu.getMenuId())) { //添加
                sysMenu.setCreateTime(now);
                sysMenu.setModifyTime(now);
                sysMenu = this.sysMenuService.insert(sysMenu);
                resultBean.setData(sysMenu);
                resultBean.setSuccess(resultBean.getData()!=null);
            } else{ //修改
                SysMenu menu = sysMenuService.queryById(sysMenu.getMenuId());
                if (menu != null) {
                    menu.setMenuName(sysMenu.getMenuName());
                    menu.setMenuUrl(sysMenu.getMenuUrl());
                    menu.setParentMenuId(sysMenu.getParentMenuId());
                    menu.setMenuOrder(sysMenu.getMenuOrder());
                    menu.setModifyTime(now);
                    sysMenu = this.sysMenuService.update(menu);
                    resultBean.setData(sysMenu);
                    resultBean.setSuccess(resultBean.getData()!=null);
                    //修改关联表
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setMenuId(sysMenu.getMenuId());
                    List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.query(roleMenu);
                    if(sysRoleMenus!=null && !sysRoleMenus.isEmpty()){
                        for(int i=0; i<sysRoleMenus.size(); i++){
                            roleMenu= sysRoleMenus.get(i);
                            roleMenu.setMenuName(sysMenu.getMenuName());
                            roleMenu.setMenuUrl(sysMenu.getMenuUrl());
                            roleMenu.setMenuOrder(sysMenu.getMenuOrder());
                            roleMenu.setParentMenuId(sysMenu.getParentMenuId());
                            roleMenu.setModifyTime(now);
                            sysRoleMenus.set(i, roleMenu);
                        }
                        sysRoleMenuService.updateBatch(sysRoleMenus);
                    }
                } else
                    resultBean.setMessage("没有找到菜单");
            }
        } else
            resultBean.setMessage("参数不能为空");
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param menuId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<SysMenu> getOne(String menuId) {
        ResultBean<SysMenu> resultBean = new ResultBean<SysMenu>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(menuId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.sysMenuService.queryById(menuId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    @PostMapping("/queryPage")
    public ResultBean<PageInfo<SysMenu>> query(@RequestBody QueryBean<SysMenu> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<SysMenu>> resultBean = new ResultBean<PageInfo<SysMenu>>();

        PageInfo<SysMenu> pageInfo = null;
        List<SysMenu> menus = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = sysMenuService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = sysMenuService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }

}